package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.omp.api.RemoteActMaintainOrderService;
import com.ruoyi.omp.api.domain.ActMaintainOrderData;
import com.ruoyi.security.domain.ScheduleTask;
import com.ruoyi.security.mapper.ScheduleTaskMapper;
import com.ruoyi.security.service.IScheduleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计划任务Service业务层处理
 *
 * @author cjie
 * @date 2022-04-11
 */
@Service
public class ScheduleTaskServiceImpl implements IScheduleTaskService {
    @Autowired
    private ScheduleTaskMapper scheduleTaskMapper;
    @Autowired
    private RemoteActMaintainOrderService remoteActMaintainOrderService;

    /**
     * 查询计划任务
     *
     * @param id 计划任务ID
     * @return 计划任务
     */
    @Override
    public ScheduleTask selectScheduleTaskById(String id) {
        return scheduleTaskMapper.selectScheduleTaskById(id);
    }

    /**
     * 查询计划任务列表
     *
     * @param scheduleTask 计划任务
     * @return 计划任务
     */
    @Override
    public List<ScheduleTask> selectScheduleTaskList(ScheduleTask scheduleTask) {
        return scheduleTaskMapper.selectScheduleTaskList(scheduleTask);
    }

    /**
     * 新增计划任务
     *
     * @param scheduleTask 计划任务
     * @return 结果
     */
    @Override
    public int insertScheduleTask(ScheduleTask scheduleTask) {
        scheduleTask.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        scheduleTask.setCreateTime(DateUtils.getNowDate());
        scheduleTask.setCreateBy(SecurityUtils.getUsername());
        // 插入表单数据
        ActMaintainOrderData orderData = new ActMaintainOrderData();
        orderData.setFormId(scheduleTask.getFormId());
        R r = remoteActMaintainOrderService.addData(orderData);
        scheduleTask.setFormData(r.getData().toString());
        return scheduleTaskMapper.insertScheduleTask(scheduleTask);
    }

    /**
     * 修改计划任务
     *
     * @param scheduleTask 计划任务
     * @return 结果
     */
    @Override
    public int updateScheduleTask(ScheduleTask scheduleTask) {
        scheduleTask.setUpdateTime(DateUtils.getNowDate());
        scheduleTask.setUpdateBy(SecurityUtils.getUsername());
        return scheduleTaskMapper.updateScheduleTask(scheduleTask);
    }

    /**
     * 批量删除计划任务
     *
     * @param ids 需要删除的计划任务ID
     * @return 结果
     */
    @Override
    public int deleteScheduleTaskByIds(String[] ids) {
        return scheduleTaskMapper.deleteScheduleTaskByIds(ids);
    }

    /**
     * 删除计划任务信息
     *
     * @param id 计划任务ID
     * @return 结果
     */
    @Override
    public int deleteScheduleTaskById(String id) {
        return scheduleTaskMapper.deleteScheduleTaskById(id);
    }
}
